<?php
namespace frontend\controllers;

use frontend\models\ArtForm;
use Yii;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;
use yii\helpers\Url;
use yii\web\Controller;
use yii\web\Response;
use yii\base\ErrorException;
use yii\helpers\Html;
use common\widgets\file_upload\FileUpload;
use common\models\member\Member;
use common\models\category\Category;
use common\models\articles\Articles;
use yii\data\Pagination;

class XiehuijsController extends Controller
{     
	public $layout = 'nomalpage';


    /**
    * 协会单页
    */
   	public function actionIndex($id){

		$cat = Category::find()->where(['status'=>1,'id'=>$id])->one();
		if($cat['parent_id']==0){
			$cat = Category::find()->where(['status'=>1,'parent_id'=>$id])->asArray()->one();
		}
		$cid=$cat['id'];
		$pid = $cat['parent_id'];
		$pcat = Category::find()->where(['status'=>1,'id'=>1])->one();

		$child = $this->cats();
		//文章内容
		$article = Articles::find()->where(['status'=>10,'category_id'=>$cid])->one();
		$hit = $article['hit']+1;
      	Yii::$app->db->createCommand("UPDATE articles SET hit = $hit WHERE id = ".$article['id'])->execute();
		//相关新闻
		$articles = Articles::find()->where(['status'=>10,'category_id'=>72])->asArray()->limit(5)->All();
        return $this->render('index',[
            'id' => $id,
            'child' => $child,
            'cat' => $cat,
            'cid' => $cid,
            'pid' => $pid,
            'pcat' => $pcat,
            'article' => $article,
            'articles' => $articles
        ]);
   	}
	/**
	 * 协会列表
	 */
	public function actionList($id){
		$child = $this->cats();
		$cat = Category::find()->where(['status'=>1,'id'=>$id])->one();
//		$pid = $cat['parent_id'];
		$pcat = Category::find()->where(['status'=>1,'id'=>1])->one();

		$query = Articles::find()->where(['category_id'=>$id]);
		$count = $query->count();
		$page = new Pagination(['totalCount' => $count, 'pageSize' => '30']);
		$articles = $query->offset($page->offset)
			->limit($page->limit)
			->all();
		return $this->render('list',[
			'id' => $id,
			'child' => $child,
			'articles' => $articles,
			'page' => $page,
			'cat' => $cat,
			'pcat' => $pcat

		]);
	}

    /**
    * 协会列表内容页
    */
   	public function actionContent($id){

		$child = $this->cats();
		$cat = Yii::$app->db->createCommand("SELECT category.* FROM category left join articles on category.id = articles.category_id where articles.id = $id")
			->queryOne();
		$pid=$cat['parent_id'];
		$articles = Yii::$app->db->createCommand("SELECT * FROM articles where status = 10 and id != $id")
			->queryAll();
		$parent=Category::find()->where(['status'=>1,'id'=>$pid])->one();
		$article = Articles::find()->where(['status'=>10,'id'=>$id])->one();
   		return $this->render('content',[
   			'article' => $article,
   			'articles' => $articles,
   			'parent' => $parent,
   			'id' => $id,
   			'child' => $child,
   			'cat' => $cat,
        ]);
   	}


	//左侧导航
	public static function cats(){
		$child = Category::find()->where(['status'=>1,'parent_id'=>1])->asArray()->All();
		foreach ($child as $key=>$cl){
			$cid = $cl['id'];
			$arr = Category::find()->where(['status'=>1,'parent_id'=>$cid])->asArray()->All();
			$child[$key]['zlm']=$arr;
		}
		return $child;
	}
}